package cn.lsg.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * 日期工具类
 * 
 * @author Administrator
 * 
 */
public class UtilDate {
	/**
	 * 此前多少天日期
	 * 
	 * @param millis
	 * @return
	 */
	public static Date howBeforeDays(long millis) {
		Date now = new Date();
		long b = now.getTime() - millis;
		return parseDate(b);
	}

	/**
	 * 日期格式化
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateFormt(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 字符串转换日期
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String date, String pattern)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.parse(date);
	}

	/**
	 * Long转换日期
	 * 
	 * @param millis
	 * @return
	 */
	public static Date parseDate(long millis) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millis);
		return cal.getTime();
	}
}
